<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/24
 * Time: 下午3:00
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\ImportModel;
class Import extends Base
{
    public function import()
    {
        if (request()->isPost()) {
            $param = input('post.');
            require_once getcwd() . '/phpexcel/Classes/PHPExcel.php';     //修改为自己的目录
            require_once getcwd() . '/phpexcel/Classes/PHPExcel/IOFactory.php';     //修改为自己的目录
            require_once getcwd() . '/phpexcel/Classes/PHPExcel/Reader/Excel5.php';     // 用于其他低版本xls
            require_once getcwd() . '/phpexcel/Classes/PHPExcel/Reader/Excel2007.php'; // 用
            echo $filePath = getcwd() . '/uploads/file/' . $param['ufile'];
            $PHPExcel = new \PHPExcel();
            /**默认用excel2007读取excel，若格式不对，则用之前的版本进行读取*/
            $PHPReader = new \PHPExcel_Reader_Excel2007();
            if (!$PHPReader->canRead($filePath)) {
                $PHPReader = new \PHPExcel_Reader_Excel5();
                if (!$PHPReader->canRead($filePath)) {
                    echo 'no Excel';
                    return;
                }
            }
            $PHPExcel = $PHPReader->load($filePath);
            $currentSheet = $PHPExcel->getSheet(0);  //读取excel文件中的第一个工作表
            $allColumn = $currentSheet->getHighestColumn(); //取得最大的列号
            $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
            $erp_orders_id = array();  //声明数组

            /**从第二行开始输出，因为excel表中第一行为列名*/
            for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
                /**从第A列开始输出*/
                for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                    $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
                    /**ord()将字符转为十进制数*/
                    $excel_data[$currentRow][] = $val;
                }
            }
            foreach ($excel_data as $key => $val) {
                $date = array(
//                    'pic' => $val[0],
                    'name' => $val[1],
                    'type' => $val[2],
                    'registration' => $val[3],
                    'approved' => $val[4],
                    'cluster' => $val[5],
                    'price' => $val[6],
                    'phone' => $val[7],
                    'create_time' => $val[8],
                    'shows' => $val[9],
                    'whetherown' => $val[10],
                );
                Db::name('auditing')->insert($date);
            }
        }
        return $this->fetch();
    }

}